package com.pateo.bjev.contacts.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pateo.bjev.contacts.R;

/**
 * Created by pateo on 17-4-25.
 */
public class TabLayout extends LinearLayout implements View.OnClickListener,ViewPager.OnPageChangeListener{

    Paint paint;

    public TabLayout(Context context) {
        super(context);
        init();
    }

    public TabLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TabLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        setWillNotDraw(false);
        setOrientation(HORIZONTAL);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.WHITE);
    }

    public Tab newTab(){
        return new Tab();
    }

    public void addTab(Tab tab){
        TextView textView = new TextView(getContext());
        textView.setText(tab.title);
        textView.setGravity(Gravity.CENTER);
        textView.setOnClickListener(this);
        textView.setTextSize(getResources().getDimensionPixelSize(R.dimen.tab_text_size));
        if(isFirstAdd()){
            textView.setTextColor(getResources().getColor(R.color.tab_selected_textcolor));
        }else{
            textView.setTextColor(getResources().getColor(R.color.tab_unselected_textcolor));
        }

        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.weight = 1;
        params.gravity = Gravity.CENTER_VERTICAL;

        addView(textView,params);
    }

    private boolean isFirstAdd(){
        return getChildCount() == 0;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(getChildCount()>selectedIndex){
            TextView view = (TextView) getChildAt(selectedIndex);

            int width = (int) getTextWidth(getContext(),view.getText().toString(), (int) view.getTextSize());
            int startX = (int) (view.getX()+(view.getWidth()-width)/2);

            canvas.drawLine(startX,view.getY()+view.getHeight()+6,startX+width,view.getY()+view.getHeight()+6,paint);
        }
    }

    private float getTextWidth(Context Context, String text, int textSize){
        TextPaint paint = new TextPaint();
        float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
        paint.setTextSize(scaledDensity * textSize);
        return paint.measureText(text);
    }

    private int selectedIndex = 0;

    @Override
    public void onClick(View v) {
        if(v instanceof TextView){
            TextView view  = (TextView) v;
            int position = this.indexOfChild(view);
            selectPosition(position);

            if(listener!=null){
                listener.onTabSelected(position);
            }
        }
    }

    private void selectPosition(int posistion){
        selectedIndex = posistion;
        TextView selectView = (TextView) getChildAt(selectedIndex);
        selectView.setTextColor(getResources().getColor(R.color.tab_selected_textcolor));
        for(int i=0;i<getChildCount();i++){
            TextView t = (TextView) getChildAt(i);
            if(i != selectedIndex){
                t.setTextColor(getResources().getColor(R.color.tab_unselected_textcolor));
            }
        }
        postInvalidate();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        selectPosition(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public class Tab{

        String title;

        protected Tab(){

        }

        public Tab setText(String title){
            this.title = title;
            return this;
        }

        public Tab setText(int res){
            String s = TabLayout.this.getContext().getString(res);
            return setText(s);
        }
    }

    private OnTabSelectedListener listener;

    public void setOnTabSelectedListener(OnTabSelectedListener listener){
        this.listener = listener;
    }

    public interface OnTabSelectedListener{
        void onTabSelected(int position);
    }
}
